import {defineStore} from "pinia";
import {onUnmounted, ref} from "vue";

export const useAnStore = defineStore('animate', () => {
    const city = ref(['hz','sx','jx','jb','jh','wx','nj'])
    const anIndex = ref(0)
    const timer = ref()

    const start = () => {
        timer.value = setInterval(() => {
            nextIndex()
        },5000)
    }

    const end = () => {
        if(timer.value) clearInterval(timer.value)
    }

    const nextIndex = () => {
        anIndex.value += 1
        if(anIndex.value > city.value.length - 1){
            anIndex.value = 0
        }
    }

    const setIndex = (index:number) => {
        anIndex.value = index
    }

    onUnmounted(()=>{
        end()
    })

    return {
        anIndex,
        nextIndex,
        setIndex,
        start,
        end
    }
})
